<template>
  <Spin :spinning="loading" tip="Loading...">
    <div>
      <!-- 搜索 -->
      <!-- 索赔申请 -->
      <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle"></TopFilter>
      <!-- table -->
      <FilterTable
        ref="table"
        :columns="columns"
        :params="fetchParams"
        :dataSource="dataList"
        :fetchapi="fetchApi"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
        :onRowSelectChange="selectChange"
        :isSelectColumn="false"
        columnsRef="claimdealerlgr"
      >
        <template slot="controls" slot-scope="props">
          <!--       <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()">添加</multiuse-button>-->
          <!--        <multiuse-button size="small">下载</multiuse-button>-->
          <multiuse-button size="small" type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
          <!--        <multiuse-button size="small">导入</multiuse-button>-->
        </template>
      </FilterTable>
      <BaseDialog :visible.sync="visible" :title="'车型选择'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <vehicle @close="close" @save="save"></vehicle>
      </BaseDialog>
      <Drawer :visible.sync="visibleEdit" :title="'索赔单明细'" destroyOnClose width="100%" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <editForm @close="editclose" @save="editsave" :rowData="rowData"></editForm>
      </Drawer>
      <Drawer :visible.sync="visibleAdd" :title="'选择经销商'" destroyOnClose width="850px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <DealerList @close="closeAdd" @save="saveAdd"></DealerList>
      </Drawer>
    </div>
  </Spin>
</template>
<script>
import moment from 'moment';
import { getSpList, getListForExport,exporturl } from '@/api/claim/dealer/ClaimQueryDealer/index';
import vehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
import editForm from './editForm.vue';
import { exportExcelFile } from '@/utils/index';
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
import { getAppId } from '@/assets/js/auth';
import { dictionary } from '@/utils/dictMixin';
export default {
  mixins: [dictionary],
  components: {
    vehicle,
    editForm,
    DealerList
  },
  data() {
    return {
      dataList: [],
      visible: false,
      visibleDrawer: false,
      collapse: true,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      fetchApi: getSpList,
      columns: this.creatHeader(),
      fetchParams: {
        techStatusStrs: '55131002,55131003, 55131004,55131005',
        moveFlag:1,
        xhrAbort: true
      }, //ipa 接口
      selectedRows: [], //选中经销商
      dialogTitle: '选择车辆',
      visibleAdd: false,
      visibleEdit: false,
      rowData: {},
      loading: false
    };
  },
  created() {
    if (getAppId() == 'arcfox') {
      this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList = this.createDictList('5543', [55431001]);
      this.topFilterInit.find(x => x.fieldName == 'ifTrailer').hidden = true
      this.columns.find(x => x.dataIndex == 'ifTrailerStr').hidden = true
    } else {
      this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList = this.createDictList('5543');
    }
    this.fetchParams.beginSubmitAt = moment()
      .startOf('week')
      .format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.endSubmitAt = moment()
      .endOf('week')
      .format('YYYY-MM-DD HH:mm:ss');
    this.chuqi();
  },
  methods: {
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '索赔单号', fieldName: 'claimNo', placeholder: '请输入索赔单号...', initialValue: '' },
        { type: 'INPUT', label: '维修工单号', fieldName: 'roNo', placeholder: '请输入维修工单号...', initialValue: '' },
        { type: 'SELECT', label: '索赔单状态', fieldName: 'claimStatus', placeholder: '请选择索赔单状态...', initialValue: '',
          itemList: this.createDictList('5513',[55131001]), filterable: true },
        { type: 'INPUT', label: '主损件代码', fieldName: 'partCode', placeholder: '请输入主损件代码...', initialValue: '' },
        { type: 'INPUT', label: '主损件名称', fieldName: 'partName', placeholder: '请输入主损件名称...', initialValue: '' },
        { type: 'SELECT', label: '索赔类型', fieldName: 'cmsClaimType', placeholder: '请选择索赔类型...', initialValue: '', filterable: true, itemList: this.createDictList('5510',[]) },
        { type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入VIN...', initialValue: '' },
        // {
        //   type: 'INPUT_CASCADER',
        //   label: '车型选择',
        //   fieldName: 'vehicleInfo',
        //   placeholder: '请选择',
        //   options: { titles: ['品牌', '车系', '车型', '配置', '选装', '颜色'] },
        //   itemList: []
        // },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelCode',
          placeholder: '请输入车型...',
          initialValue: '',
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          }
        },
        { type: 'INPUT', label: '车牌号', fieldName: 'licenseNo', placeholder: '请输入车牌号...', initialValue: '' },
        // { type: 'SELECT', label: '动力形式', fieldName: 'ifComm', placeholder: '请选择动力形式...', initialValue: '', itemList: [] },
        // { type: 'INPUT', label: '批量活动名称', fieldName: 'batchName', placeholder: '请输入', initialValue: '' },
        // {
        //   type: 'RANGE_DATE',
        //   label: '申请日期',
        //   fieldName: 'beginCreatedAt|endCreatedAt',
        //   initialValue: [
        //     moment()
        //       .startOf('week')
        //       .format('YYYY-MM-DD HH:mm:ss'),
        //     moment()
        //       .endOf('week')
        //       .format('YYYY-MM-DD HH:mm:ss')
        //   ],
        //   placeholder: '请输入',
        //   rules: [{ required: false, message: '请选择日期', trigger: 'change' }]
        // },
        {
          type: 'RANGE_DATE',
          label: '提交日期',
          fieldName: 'beginSubmitAt|endSubmitAt',
          initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ],
          placeholder: '请输入',
          rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
      ];
    },
    //搜索获取数据
    changeHandle(val) {
      if (!val.beginSubmitAt) {
        this.$notify.warning({ title: '提示信息', message: '提交开始日期不可为空！' });
        return;
      }
      if (!val.endSubmitAt) {
        this.$notify.warning({ title: '提示信息', message: '提交结束日期不可为空！' });
        return;
      }
      if (getAppId() == 'arcfox') {
        if (moment(moment(val.endSubmitAt).format('YYYY-MM-DD')).diff(moment(val.beginSubmitAt), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
          return;
        }
      } else {
        if (moment(moment(val.endSubmitAt).format('YYYY-MM-DD')).diff(moment(val.beginSubmitAt), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
          return;
        }
      }
      this.fetchParams = Object.assign({}, this.fetchParams, val, {
        techStatusStrs: '55131002,55131003, 55131004,55131005',
        moveFlag:1,
        xhrAbort: false
      });
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 80,
          render: props => {
            return (
              <div style="text-align:center">
                <multiuse-button type="text" onClick={() => this.edit(props.row)}>
                  明细
                </multiuse-button>
              </div>
            );
          }
        },
        //   索赔单状态、索赔单号、索赔类型、预授权单号、维修单号、VIN、车牌号、车型名称、主损件代码、主损件名称、行驶里程、故障现象代码、
        // 故障现象名称、故障原因代码、故障原因名称、索赔金额、
        { title: '索赔单状态', dataIndex: 'statusStr', sorter: true },
        { title: '索赔单号', dataIndex: 'claimNo', sorter: true },
        // { title: '动力形式', dataIndex: 'ifCommStr', sorter: true },
        // { title: '是否拖车', dataIndex: 'ifTrailerStr', sorter: true },
        { title: '索赔类型', dataIndex: 'cmsClaimTypeStr', sorter: true },
        { title: '预授权单号', dataIndex: 'authNo', sorter: true },
        { title: '维修工单号', dataIndex: 'roNo', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '车牌号', dataIndex: 'licenseNo', sorter: true },
        { title: '车型名称', dataIndex: 'modelName', sorter: true },
        { title: '主损件代码', dataIndex: 'partCode', sorter: true },
        { title: '主损件名称', dataIndex: 'partName', sorter: true },
        {title: '供应商', dataIndex: 'supName', sorter: true},
        { title: '行驶里程', dataIndex: 'driveMile', sorter: true },
        { title: '故障现象代码', dataIndex: 'faultCode', sorter: true },
        { title: '故障现象名称', dataIndex: 'faultName', sorter: true },
        { title: '故障原因代码', dataIndex: 'frCode', sorter: true },
        { title: '故障原因名称', dataIndex: 'frName', sorter: true },
        // { title: '批量活动名称', dataIndex: 'batchName', sorter: true },
        // { title: '驳回次数', dataIndex: 'channelNum', sorter: true },
        // { title: '是否一口价', dataIndex: 'ifPrice', sorter: true },
        // { title: '一口价金额', dataIndex: 'priceAmount', sorter: true },
        { title: '索赔金额', dataIndex: 'amount', sorter: true ,
          render: props => {
            return <span>{props.row.priceAmount==0?props.row.sumAmount:props.row.priceAmount}</span>;
          }},

        { title: '工时费', dataIndex: 'labourAmount', sorter: true },
        { title: '备件费', dataIndex: 'partAmount', sorter: true },
        { title: '附加费用', dataIndex: 'addAmount', sorter: true },
        // { title: '最终审核备注', dataIndex: 'checkRemark', sorter: true },
        { title: '保修起始日期', dataIndex: 'repairBeginDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '保修截止日期', dataIndex: 'repairEndDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '生产日期', dataIndex: 'prodDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '销售日期', dataIndex: 'saleDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '维修日期', dataIndex: 'serviceBeginDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
        { title: '申请人', dataIndex: 'applyBy', sorter: true },
        { title: '申请时间', dataIndex: 'applyAt', sorter: true, width: 140 },
        { title: '审核人', dataIndex: 'checkBy', sorter: true },
        { title: '审核时间', dataIndex: 'checkAt', sorter: true },
      ];
    },
    async chuqi() {

    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    close() {
      this.visible = false;
    },
    chooseOrderNo() {
      this.visible = true;
    },
    chooseDealerNo() {
      this.visibleAdd = true;
    },
    saveAdd(val) {
      this.topFilterInit.find(x => x.fieldName == 'ownerCode').initialValue = val[0].dealerCode;
      this.dealer = { ...val[0] };
      this.visibleAdd = false;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    save(obj) {
      this.topFilterInit.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
      this.visible = false;
    },
    async exportFileBtn() {
      // let json = Object.assign({}, { CODE: 'CODE_00000203' }, this.fetchParams);
      // let res = await exporturl(json);
      // if (res.resultCode == 200) {
      //   this.$router.push('/admin/auth/oem/downloadCenter');
      // }
      let beginSubmitAt = this.topFilterInit.find(x => x.fieldName == 'beginSubmitAt|endSubmitAt').initialValue[0];
      let endSubmitAt = this.topFilterInit.find(x => x.fieldName == 'beginSubmitAt|endSubmitAt').initialValue[1];
      if (!beginSubmitAt) {
        this.$notify.warning({ title: '提示信息', message: '提交开始日期不可为空！' });
        return;
      }
      if (!endSubmitAt) {
        this.$notify.warning({ title: '提示信息', message: '提交结束日期不可为空！' });
        return;
      }
      if (getAppId() == 'arcfox') {
        if (moment(moment(endSubmitAt).format('YYYY-MM-DD')).diff(moment(beginSubmitAt), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
          return;
        }
      } else {
        if (moment(moment(endSubmitAt).format('YYYY-MM-DD')).diff(moment(beginSubmitAt), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
          return;
        }
      }
      let obj = Object.assign({}, this.fetchParams, { techStatusStrs: '55131002,55131003, 55131004,55131005', xhrAbort: false });
      obj.beginSubmitAt = this.topFilterInit.find(x => x.fieldName == 'beginSubmitAt|endSubmitAt').initialValue[0];
      obj.endSubmitAt = this.topFilterInit.find(x => x.fieldName == 'beginSubmitAt|endSubmitAt').initialValue[1];
      this.loading = true;
      getListForExport(obj).then(res => {
        exportExcelFile(res.data, '索赔单信息');
        this.loading = false;
      });
    },
    add() {
      this.visibleAdd = true;
    },
    // saveAdd() {
    //   this.visibleAdd = false;
    //   this.fetchParams = { ...this.fetchParams };
    // },
    editclose() {
      this.visibleEdit = false;
    },
    edit(val) {
      this.rowData = val;
      this.visibleEdit = true;
    },
    editsave() {
      this.visibleEdit = false;
      this.fetchParams = { ...this.fetchParams };
    }
  }
  // mounted() {
  //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
  // }
};
</script>

<style lang="less" scoped></style>
